package com.gewu.teday.activity

import android.annotation.SuppressLint
import android.text.InputType
import android.util.Log
import android.view.Gravity
import android.view.View
import android.widget.*
import androidx.recyclerview.widget.LinearLayoutManager
import com.gewu.teday.R
import com.gewu.teday.adapter.TixianListAdapter
import com.gewu.teday.adapter.TixianListBean
import com.gewu.teday.bean.TiXianItemBean
import com.gewu.teday.utils.GwDialog
import com.gewu.teday.utils.SpUtils
import com.gewu.teday.utils.Util
import kotlinx.android.synthetic.main.activity_tixianlist.*
import kotlinx.android.synthetic.main.toolbar_layout.*

class TixianListActivity : BaseActivity(), TixianListAdapter.Onclick {


    override fun getLayout(): Int = R.layout.activity_tixianlist

    override fun initView() {
        toolbar_content.visibility = View.VISIBLE
        tixian_list.visibility = View.VISIBLE
        toolbar_main.visibility = View.GONE
        toolbar_money.title = ""
        setSupportActionBar(toolbar_money)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        toolbar_money.setNavigationOnClickListener(View.OnClickListener { finish() })
        initAdapter()
    }

    private var tixianListAdapter: TixianListAdapter? = null

    private fun initAdapter() {
        tixianListAdapter = TixianListAdapter()
        listview.adapter = tixianListAdapter
        listview.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
    }

    override fun initData() {
        val decodeParcelable = SpUtils.instances!!.decodeParcelable("banck", TixianListBean::class.java)
        addListItem.setOnLongClickListener(object :View.OnLongClickListener{
            override fun onLongClick(p0: View?): Boolean {
                showAddWindow()
                return true
            }
        })
        setAdapter(decodeParcelable)
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun setAdapter(decodeParcelable: TixianListBean?) {
        if (decodeParcelable != null) {
            for (i in 0 until decodeParcelable.list.size) {
                tixianListAdapter?.addList(decodeParcelable.list[i])
            }
            tixianListAdapter?.setClick(this)
            tixianListAdapter?.notifyDataSetChanged()
        }
    }


    @SuppressLint("NotifyDataSetChanged")
    private fun showAddWindow() {
        val build = GwDialog(this).LayoutBuilder()
        val create = build.create(R.layout.teday_window, false, Gravity.CENTER, R.drawable.shape)
        create.show()
        val viewlayout = build.getView()
        val cancel = viewlayout?.findViewById<ImageView>(R.id.cancel)
        viewlayout?.findViewById<LinearLayout>(R.id.top_contunt)!!.visibility = View.GONE
        viewlayout.findViewById<LinearLayout>(R.id.bankNum)!!.visibility = View.VISIBLE
        viewlayout.findViewById<LinearLayout>(R.id.bankTime)!!.visibility = View.VISIBLE
        viewlayout.findViewById<TextView>(R.id.textname)!!.text = "添加一条"
        viewlayout.findViewById<TextView>(R.id.bank_text).text = "设置银行"
        viewlayout.findViewById<TextView>(R.id.bank_money).text = "设置金额"

        val chuangzuoshouyi = viewlayout.findViewById<EditText>(R.id.chuangzuoshouyi)
        val jichushouyi = viewlayout.findViewById<EditText>(R.id.jichushouyi)
        val banketnum = viewlayout.findViewById<EditText>(R.id.banketnum)
        val banketTime = viewlayout.findViewById<EditText>(R.id.banketTime)
        chuangzuoshouyi.inputType = InputType.TYPE_CLASS_TEXT
        val sucess = viewlayout.findViewById<TextView>(R.id.sucess)

        cancel?.setOnClickListener {
            create.dismiss()
        }
        val decodeParcelable =
            SpUtils.instances!!.decodeParcelable("banck", TixianListBean::class.java)
        val list = decodeParcelable?.list;
        if (decodeParcelable != null && list != null) {
            if (list.size != 0) {
                chuangzuoshouyi.hint = list.last().bankName.toString()
                jichushouyi.hint = list.last().bankMoney.toString()
                banketnum.hint = list.last().bankNum.toString()
                banketTime.hint = list.last().time
            }
        } else {
            jichushouyi.hint = "如：123.12元"
            chuangzuoshouyi.hint = "中国银行/南京银行"
            banketTime.hint = "如：2022-08-10 10:20:00"
        }

        sucess.setOnClickListener {
            val name = if (chuangzuoshouyi.text.isNullOrEmpty()) {
                if (decodeParcelable != null && !(list.isNullOrEmpty())) {
                    list.last().bankName
                } else {
                    ""
                }
            } else {
                chuangzuoshouyi.text.toString()
            }

            val money = if (jichushouyi.text.isNullOrEmpty()) {
                if (decodeParcelable != null && !(list.isNullOrEmpty())) {
                    list.last().bankMoney
                } else {
                    ""
                }
            } else {
                jichushouyi.text.toString()
            }

            val num = if (banketnum.text.isNullOrEmpty()) {
                if (decodeParcelable != null && !(list.isNullOrEmpty())) {
                    list.last().bankNum
                } else {
                    ""
                }
            } else {
                banketnum.text.toString()
            }

            val time = if (banketTime.text.isNullOrEmpty()) {
                if (decodeParcelable != null && !(list.isNullOrEmpty())) {
                    list.last().time
                } else {
                    ""
                }
            } else {
                banketTime.text.toString()
            }


            if (decodeParcelable == null || list.isNullOrEmpty()) {
                if ((name.isEmpty() || money.isEmpty() || num.isEmpty()) || time.isEmpty()) {
                    Toast.makeText(this, "请输入", Toast.LENGTH_SHORT).show()
                    return@setOnClickListener
                }
            }

            if (money.toFloat()==0F){
                Toast.makeText(this,"输入不能为0元",Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if (time.length == 14) {
                if (Util.isValidDate(Util.StringToDate(time))) {
                    val tiXianItemBean: TiXianItemBean = TiXianItemBean(
                        name.toString(),
                        money.toString(),
                        Util.StringToDate(time),
                        num.toString()
                    )

                    if (list != null) {
                        tiXianItemBean.let { it1 ->
                            tixianListAdapter?.addList(it1)
                            list.add(it1)
                        }
                        SpUtils.instances?.encodeParcelable("banck", decodeParcelable)
                    } else {
                        val lists = ArrayList<TiXianItemBean>()
                        tiXianItemBean.let { it1 ->
                            tixianListAdapter?.addList(it1)
                            lists.add(it1)
                        }

                        SpUtils.instances?.encodeParcelable("banck", TixianListBean(lists))
                    }
                    tixianListAdapter?.notifyDataSetChanged()
                    create.dismiss()
                } else {
                    Toast.makeText(this, "日期输入不合法", Toast.LENGTH_SHORT).show()
                }
            } else
                Toast.makeText(this, "日期长度异常(正常14位)", Toast.LENGTH_SHORT).show()
        }
    }

    @SuppressLint("NotifyDataSetChanged", "NewApi")
    override fun remove(index: String) {
        val decodeParcelable = SpUtils.instances?.decodeParcelable("banck", TixianListBean::class.java)
        decodeParcelable?.list?.removeIf {
            it.time == index
        }
        SpUtils.instances?.encodeParcelable("banck", decodeParcelable)
        tixianListAdapter?.notifyDataSetChanged()
    }
}